
package services;

import dao.TestcaseJpaController;
import dao.exceptions.NonexistentEntityException;
import entities.Problem;
import entities.Testcase;
import exceptions.DatabaseException;
import exceptions.NonExistentObjectException;
import java.util.ArrayList;
import java.util.List;
import vo.VOTestcase;

/**
 *
 * @author sbaldrich
 */
public class TestcaseService {

    private static TestcaseJpaController tc;
    public static TestcaseJpaController getInstance()
    {
        if(tc == null)
            tc = new TestcaseJpaController();
        return tc;
    }

    private static ProblemService ps;
    public static ProblemService getProblemService()
    {
        if(ps == null)
            ps = new ProblemService();
        return ps;
    }


    public void save(VOTestcase vo) throws Exception
    {
        tc = getInstance();
        ps = getProblemService();
        Testcase t = new Testcase(vo);
        try
        {
            t.setProblem(ps.findObject(vo.getProblemCode()));
            tc.create(t);
        } catch (Exception e)
        {
            if(e instanceof NonExistentObjectException)
                throw new NonExistentObjectException("Problem");
            throw new DatabaseException("TestcaseService.save");
        }
    }

    public VOTestcase find(VOTestcase vo)throws Exception
    {
        tc = getInstance();
        try
        {
            return tc.findTestcase(vo.getIdTestCase()).getVO();
        } catch (Exception e)
        {
            throw e;
        }
    }

    public void update(VOTestcase vo)throws Exception
    {
        tc = getInstance();
        ps = getProblemService();
        Testcase t = new Testcase(vo);
        try
        {
            t.setProblem(ps.findObject(vo.getProblemCode()));
            tc.edit(t);
        } catch (Exception e)
        {
            if(e instanceof NonexistentEntityException)
            {
                throw new NonExistentObjectException();
            }
            throw new DatabaseException("TestcaseService.update Operation");
        }
    }

    public void delete(VOTestcase vo)throws Exception
    {
        tc = getInstance();
        try
        {
            tc.destroy(vo.getIdTestCase());
        } catch (Exception e)
        {
            if( e instanceof NonexistentEntityException)
            {
                throw new NonExistentObjectException();
            }
            throw new DatabaseException("TestcaseService.delete Operation");
        }
    }

    ArrayList<VOTestcase> find(String problemCode) throws Exception
    {
        ps = getProblemService();
        try
        {
            Problem p = ps.findObject(problemCode);
            if(p == null)
                throw new NonExistentObjectException("Problem");
            List<Testcase> l = p.getTestcaseList();
            if(l == null ||l.isEmpty())
                throw new NonExistentObjectException("Testcases");
            ArrayList<VOTestcase> ret = new ArrayList<VOTestcase>();
            for( Testcase t : l)
                ret.add(t.getVO());
            return ret;
        }
        catch (Exception e)
        {
            throw e;
        }
    }
}
